package com.twinkle.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.twinkle.dao.ClusterResultMapper;
import com.twinkle.model.ClusterResult;
import com.twinkle.service.ClusterResultService;
import com.twinkle.utils.JqgridUtil;
import com.twinkle.utils.ParamTranslate;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.util.ArrayList;
import java.util.List;

/**
 * @title: ClusterResultService
 * @description:
 * @author: Paul
 * @date: 2023/3/13 19:06
 */
@Service
public class ClusterResultServiceImpl implements ClusterResultService {

    @Autowired
    ClusterResultMapper mapper;
    @Autowired
    private JqgridUtil jqgridUtil;

    @Override
    public ClusterResult getClusterResultByModelId(String modelId) {
        ClusterResult algoResult = mapper.selectOne(new QueryWrapper<ClusterResult>().eq("model_id", modelId));
        String twoDimDisImage = algoResult.getTwoDimDisImage();
        //格式化param
        StringBuffer paramFormat = new StringBuffer();
        String param = algoResult.getParam();
        JSONObject paramObject = JSONObject.parseObject(param);
        for (String key : paramObject.keySet()) {
            paramFormat.append(ParamTranslate.getParamName(key) + " : " + paramObject.getString(key) + "\r\n");
        }
        algoResult.setParam(paramFormat.toString());

        //截取图片名称
        if (StringUtils.isNotBlank(twoDimDisImage)) {
            String[] splitTwoDimDisImage = twoDimDisImage.split("\\/");
            twoDimDisImage=splitTwoDimDisImage[splitTwoDimDisImage.length-1];
            algoResult.setTwoDimDisImage(twoDimDisImage);
        }
        String predDisImage = algoResult.getPredDisImage();
        if (StringUtils.isNotBlank(predDisImage)) {
            String[] splitPredDisImage = predDisImage.split("\\/");
            predDisImage=splitPredDisImage[splitPredDisImage.length-1];
            algoResult.setPredDisImage(predDisImage);
        }
        return algoResult;
    }

    @Override
    public ClusterResult getClusterResultById(String id) {
        ClusterResult algoResult = mapper.selectById(id);
        //格式化param
        StringBuffer paramFormat = new StringBuffer();
        String param = algoResult.getParam();
        JSONObject paramObject = JSONObject.parseObject(param);
        for (String key : paramObject.keySet()) {
            paramFormat.append(ParamTranslate.getParamName(key) + " : " + paramObject.getString(key) + "/r/n");
        }
        algoResult.setParam(paramFormat.toString());
        //截取图片名称
        String twoDimDisImage = algoResult.getTwoDimDisImage();
        if (StringUtils.isNotBlank(twoDimDisImage)) {
            String[] splitTwoDimDisImage = twoDimDisImage.split("\\/");
            twoDimDisImage=splitTwoDimDisImage[splitTwoDimDisImage.length-1];
            algoResult.setTwoDimDisImage(twoDimDisImage);
        }
        String predDisImage = algoResult.getPredDisImage();
        if (StringUtils.isNotBlank(predDisImage)) {
            String[] splitPredDisImage = predDisImage.split("\\/");
            predDisImage=splitPredDisImage[splitPredDisImage.length-1];
            algoResult.setPredDisImage(predDisImage);
        }
        return algoResult;
    }

    @Override
    public String getAllClusterResultJson(int page, int count) {
        List<ClusterResult> all = mapper.selectList(null);
        int toIndex = count * page;
        if (all.size() < toIndex) {
            toIndex = all.size();
        }
        List<ClusterResult> list = all.subList(count * (page - 1), toIndex);
        List<ClusterResult> listNew = new ArrayList<>();
        for (ClusterResult clusterResult : list) {
            String twoDimDisImage = clusterResult.getTwoDimDisImage();
            if (StringUtils.isNotBlank(twoDimDisImage)) {
                String[] splitTwoDimDisImage = twoDimDisImage.split("\\/");
                twoDimDisImage=splitTwoDimDisImage[splitTwoDimDisImage.length-1];
                clusterResult.setTwoDimDisImage(twoDimDisImage);
            }
            String predDisImage = clusterResult.getPredDisImage();
            if (StringUtils.isNotBlank(predDisImage)) {
                String[] splitPredDisImage = predDisImage.split("\\/");
                predDisImage=splitPredDisImage[splitPredDisImage.length-1];
                clusterResult.setPredDisImage(predDisImage);
            }
            listNew.add(clusterResult);
        }
        return jqgridUtil.getJson(listNew, page + "", all.size() , count);
    }

    @Override
    public String getAllClusterResultUIJson(int page, int count) {
        return null;
    }

    @Override
    public String clusterResultListForUserAdd() {
        return null;
    }

    @Override
    public String addClusterResult(ClusterResult algoResult) throws ParseException {
        return null;
    }

    @Override
    public String modifyClusterResult(ClusterResult algoResult) {
        return null;
    }

    @Override
    public String deleteClusterResult(String id) {
        return null;
    }

    @Override
    public List<ClusterResult> getAllClusterResult() {
        return null;
    }

    @Override
    public String getAllClusterResultJsonBySearch(int page, int count, String algoName, String appName) {
        QueryWrapper<ClusterResult> clusterResultQueryWrapper = new QueryWrapper<>();
        if (StringUtils.isNotBlank(algoName)) {
            clusterResultQueryWrapper.eq("algo_name", algoName);
        }
        if (StringUtils.isNotBlank(appName)) {
            clusterResultQueryWrapper.eq("app_name", appName);
        }
        List<ClusterResult> all = mapper.selectList(clusterResultQueryWrapper);
        int toIndex = count * page;
        if (all.size() < toIndex) {
            toIndex = all.size();
        }
        List<ClusterResult> list = all.subList(count * (page - 1), toIndex);
        List<ClusterResult> listNew = new ArrayList<>();
        for (ClusterResult clusterResult : list) {
            String twoDimDisImage = clusterResult.getTwoDimDisImage();
            if (StringUtils.isNotBlank(twoDimDisImage)) {
                String[] splitTwoDimDisImage = twoDimDisImage.split("\\/");
                twoDimDisImage=splitTwoDimDisImage[splitTwoDimDisImage.length-1];
                clusterResult.setTwoDimDisImage(twoDimDisImage);
            }
            String predDisImage = clusterResult.getPredDisImage();
            if (StringUtils.isNotBlank(predDisImage)) {
                String[] splitPredDisImage = predDisImage.split("\\/");
                predDisImage=splitPredDisImage[splitPredDisImage.length-1];
                clusterResult.setPredDisImage(predDisImage);
            }
            listNew.add(clusterResult);
        }
        return jqgridUtil.getJson(listNew, page + "", all.size() , count);
    }

}


